package com.yxzr.youyou.fragment.vip;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yxzr.youyou.R;
import com.yxzr.youyou.adapter.ComicRecyclerViewAdapter;
import com.yxzr.youyou.adapter.ComicTypeAdapter;
import com.yxzr.youyou.base.BaseFragment;
import com.yxzr.youyou.bean.TwoTypeResBean;
import com.yxzr.youyou.bean.TypeBean;
import com.yxzr.youyou.retrofit.BaseObserver;
import com.yxzr.youyou.retrofit.RequestUtil;
import com.yxzr.youyou.utils.NullStringToEmptyAdapterFactory;
import com.yxzr.youyou.utils.RecyclerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class VipComicFragment extends BaseFragment {
    private RecyclerView mComicAddress,mComicYear,mComicLanuage,mComicMovie;
    private ComicTypeAdapter adapter1,adapter2,adapter3;
    private ComicRecyclerViewAdapter adapter;
    private RadioGroup comic_rg;
    private List<TypeBean.DataBeanX.DataBean> addressList = new ArrayList<>();
    private List<TypeBean.DataBeanX.DataBean> yearList = new ArrayList<>();
    private List<TypeBean.DataBeanX.DataBean> lanuageList = new ArrayList<>();
    private int addressIndex = 0,yearIndex = 0,lanuageIndex = 0;
    private int index = 2;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.frag_comic,null);
        initView(v);
        return v;
    }

    private void initView(View v) {
        mComicAddress = v.findViewById(R.id.comic_rv_address);
        mComicYear = v.findViewById(R.id.comic_rv_year);
        mComicLanuage = v.findViewById(R.id.comic_rv_language);
        mComicMovie = v.findViewById(R.id.comic_rv_movie);
        comic_rg = v.findViewById(R.id.comic_rg);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mComicAddress.addItemDecoration(new RecyclerItemDecoration(10,1));
        mComicAddress.setLayoutManager(linearLayoutManager);
        adapter1 = new ComicTypeAdapter(getContext());
        mComicAddress.setAdapter(adapter1);

        LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(getContext());
        linearLayoutManager1.setOrientation(LinearLayoutManager.HORIZONTAL);
        mComicYear.addItemDecoration(new RecyclerItemDecoration(10,1));
        mComicYear.setLayoutManager(linearLayoutManager1);
        adapter2 = new ComicTypeAdapter(getContext());
        mComicYear.setAdapter(adapter2);

        LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(getContext());
        linearLayoutManager2.setOrientation(LinearLayoutManager.HORIZONTAL);
        mComicLanuage.addItemDecoration(new RecyclerItemDecoration(10,1));
        mComicLanuage.setLayoutManager(linearLayoutManager2);
        adapter3 = new ComicTypeAdapter(getContext());
        mComicLanuage.setAdapter(adapter3);

        GridLayoutManager layoutManager = new GridLayoutManager(getContext(),3);
        mComicMovie.setLayoutManager(layoutManager);
        mComicMovie.addItemDecoration(new RecyclerItemDecoration(10,3));
        mComicMovie.setNestedScrollingEnabled(false);
        adapter = new ComicRecyclerViewAdapter(getContext(),1);
        mComicMovie.setAdapter(adapter);

        adapter1.setOnClickListener(new ComicTypeAdapter.OnClickListener() {
            @Override
            public void onClick(int position) {
                addressIndex = position;
                adapter1.setSelectIndex(position);
                initData();
            }
        });

        adapter2.setOnClickListener(new ComicTypeAdapter.OnClickListener() {
            @Override
            public void onClick(int position) {
                yearIndex = position;
                adapter2.setSelectIndex(position);
                initData();
            }
        });

        adapter3.setOnClickListener(new ComicTypeAdapter.OnClickListener() {
            @Override
            public void onClick(int position) {
                lanuageIndex = position;
                adapter3.setSelectIndex(position);
                initData();
            }
        });

        comic_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.comic_rb_one:
                        index = 2;
                        initData();
                        break;
                    case R.id.comic_rb_two:
                        index = 1;
                        initData();
                        break;
                }
            }
        });
        initType();
    }

    private void initType(){
        RequestUtil.getInstence().API().homeType("1")
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
                .subscribe(new BaseObserver(getActivity(),true){
                    @Override
                    protected void onSuccees(String t) {
                        Gson gson = new GsonBuilder().registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory()).create();
                        TypeBean bean = gson.fromJson(t,TypeBean.class);

                        if (bean.getData() != null && bean.getData().size() >= 3){
                            if (bean.getData().get(0).getData() != null && bean.getData().get(0).getData().size() > 0){
                                addressList = bean.getData().get(0).getData();
                                adapter1.addList(bean.getData().get(0).getData());
                            }
                            if (bean.getData().get(1).getData() != null && bean.getData().get(1).getData().size() > 0){
                                yearList = bean.getData().get(1).getData();
                                adapter2.addList(bean.getData().get(1).getData());
                            }
                            if (bean.getData().get(2).getData() != null && bean.getData().get(2).getData().size() > 0){
                                lanuageList = bean.getData().get(2).getData();
                                adapter3.addList(bean.getData().get(2).getData());
                            }
                        }

                    }

                    @Override
                    protected void onFail(int error_code) {

                    }

                    @Override
                    protected void onComp() {
                        initData();
                    }

                    @Override
                    protected void onErr(Throwable e) {

                    }
                });
    }


    private void initData(){
        Log.e("TAG","addressList:"+addressList.get(addressIndex).getTitle());
        Log.e("TAG","lanuageList:"+lanuageList.get(lanuageIndex).getTitle());
        Log.e("TAG","yearList:"+yearList.get(yearIndex).getTitle());
        Log.e("TAG","index:"+index);
        RequestUtil.getInstence().API().movieSearch("1","55",addressList.get(addressIndex).getTitle(),
                lanuageList.get(lanuageIndex).getTitle(),yearList.get(yearIndex).getTitle(),index+"","")
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
                .subscribe(new BaseObserver(getActivity(),true){
                    @Override
                    protected void onSuccees(String t) {
                        Gson gson = new GsonBuilder().registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory()).create();
                        TwoTypeResBean bean = gson.fromJson(t,TwoTypeResBean.class);

                        adapter.clearList();
                        adapter.addList(bean.getData());
                    }

                    @Override
                    protected void onFail(int error_code) {
                        adapter.clearList();
                    }

                    @Override
                    protected void onComp() {

                    }

                    @Override
                    protected void onErr(Throwable e) {

                    }
                });
    }
}
